package com.example.areaclean;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class stu_score_activity extends AppCompatActivity {
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    RequestQueue queue;
    private EditText editText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stu_score);


        pref=getSharedPreferences("data",MODE_PRIVATE);
        queue = Volley.newRequestQueue(getApplicationContext());
        jsonRequest(pref.getString("s_no",""));
        TextView textView_1=findViewById(R.id.text_1);
        textView_1.setText(pref.getString("s_name1",""));
        TextView textView_2=findViewById(R.id.text_2);
        textView_2.setText(pref.getString("Stu_area",""));

        editText=findViewById(R.id.edit_appeal);
        Button appeal=findViewById(R.id.btn_appeal);

        appeal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s=editText.getText().toString();
                jsonRequest2(pref.getString("s_no",""),s);
            }
        });


    }

    public void jsonRequest(String s_no){
        String url= "http://192.168.31.32/tp/public/index.php/api/stu_getScore";

        HashMap<String, String> params = new HashMap<>();
        params.put("s_no",s_no);
        JSONObject jsonObject = new JSONObject(params);
        Log.d("ABBBBBB",jsonObject.toString());

        JsonObjectRequest jsonObjectRequest =new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        String b="0";
                        String a="0";
                        try {
                            b= jsonObject.getString("stu_score");
                            a=jsonObject.getString("time");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        TextView textView_3=findViewById(R.id.text_3);
                        textView_3.setText(a);
                        TextView textView_4=findViewById(R.id.text_4);
                        textView_4.setText(b);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("TAG","fuckError"+volleyError);
                    }
                });
        queue.add(jsonObjectRequest);

    }
    public void jsonRequest2(String s_no,String text){
        String url= "http://192.168.31.32/tp/public/index.php/api/appeal";
        HashMap<String, String> params = new HashMap<>();
        params.put("s_no",s_no);
        params.put("a_text",text);
        JSONObject jsonObject = new JSONObject(params);
        Log.d("ABBBBBB",jsonObject.toString());
        JsonObjectRequest jsonObjectRequest =new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        String a="0";
                        try {
                            a=jsonObject.getString("appeal");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (a.equals("successful")){
                            Toast.makeText(stu_score_activity.this,"申诉成功",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(stu_score_activity.this,"申诉失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("TAG","fuckError"+volleyError);
                    }
                });
        queue.add(jsonObjectRequest);
    }
}
